package com.platform.modules.prj.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.platform.common.annotation.SysLog;
import com.platform.common.page.PageUtil;
import com.platform.common.utils.RestResponse;
import com.platform.modules.cpt.entity.CptAcceptanceConditionEntity;
import com.platform.modules.prj.entity.PrjPauseEntity;
import com.platform.modules.prj.entity.PrjProjectEntity;
import com.platform.modules.prj.service.PrjPauseService;
import com.platform.modules.sys.controller.AbstractController;
import com.platform.modules.sys.entity.SysOrgEntity;
import com.platform.modules.sys.entity.SysUserEntity;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * Controller
 *
 * @author biaohuaming
 * @date 2020-03-11
 */
@Api(tags = "PrjPauseController|中止施工")
@RestController
@RequestMapping("prj/pause")
public class PrjPauseController extends AbstractController {
    @Autowired
    private PrjPauseService prjPauseService;

    /**
     * @descr 查询 中止施工记录, 查询本机构<br>
     * @param currPage
     * @param limit
     * @return
     */
    @ApiOperation(value = "查询 中止施工记录 PC")
    @PostMapping("/list")
    public RestResponse query(Integer currPage, Integer limit, @RequestParam(required = false) String prjName, 
    		@RequestParam(required = false) String cityNum, @RequestParam(required = false) String countyNum,
    		@RequestParam(required = false) String[] status) {
    	Map<String, Object> params = new HashMap<>();
    	params.put("buildCorpId", getOrgNo());
        params.put("page", PageUtil.getCurrPage(currPage));
        params.put("limit", PageUtil.getLimit(limit));
        params.put("cityNum", cityNum);
        params.put("countyNum", countyNum);
        params.put("prjName", prjName);
        params.put("status", status);
    	Page<PrjPauseEntity> page = prjPauseService.queryPage(params);
        return RestResponse.success().put("page", page);
    } 
    
    /**
     * @descr 中止施工申请-详情
     * @param id
     * @return
     */
    @ApiOperation(value = "中止施工申请-详情")
    @GetMapping("/info")
    public RestResponse view(@RequestParam String pauseId) {
    	PrjPauseEntity entity = prjPauseService.getById(pauseId);
        return RestResponse.success().put("data", entity);
    } 
    
    @ApiOperation(value = "项目中止施工记录", notes = "项目中止施工记录")
    @SysLog("创建中止施工记录")
    @PostMapping("/save")
    public RestResponse save(@Valid PrjPauseEntity prjPause) {
    	SysOrgEntity sysOrgEntity = this.getOrg();
    	SysUserEntity sysUserEntity = this.getUser();
    	
    	//状态 0-未申请 1-已申请 2-已驳回 3-已同意
    	if(null==prjPause.getStatus()) {
        	prjPause.setStatus(0);
        	prjPause.setBuildCorpId(sysOrgEntity.getOrgNo());
        	prjPause.setBuildCorpName(sysOrgEntity.getOrgName());
        	prjPause.setCreateTime(new Date());
        	prjPause.setCreateUserId(sysUserEntity.getUserId());
        	prjPause.setCreateUserName(sysUserEntity.getRealName());
    	}else {
    		//更新保存时, 因为页面格式化显示原因, 创建时间被修改. 因此赋值为空不修改创建时间
    		prjPause.setCreateTime(null);
    		prjPause.setUpdateTime(new Date());
    	}
        prjPauseService.saveOrUpdate(prjPause);
        return RestResponse.success().put("prjPause", prjPause);
    }
    
    @ApiOperation(value = "保存中止施工审批信息", notes = "保存中止施工审批信息")
    @SysLog("保存中止施工审批信息")
    @PostMapping("/sp")
    public RestResponse sp(PrjPauseEntity prjPause) {
    	SysOrgEntity sysOrgEntity = this.getOrg();
    	SysUserEntity sysUserEntity = this.getUser();
    	
    	PrjPauseEntity queryEntity = prjPauseService.sp(prjPause, sysOrgEntity, sysUserEntity);
        return RestResponse.success().put("prjPause", queryEntity);
    }
}
